import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys: [],
		
		// 搜索表单列表
		forms : [
			{ label: '公司',		name: 'code' },
		],
		// 提交表单列表
		formData : [
			{ label: '分公司',		name: 'vendor_comp_id', 	half:true,	type: 'select', loading:true,	required:true,
				onChange:({value}) => {
					// 获取送检单位
					if (value) {
						this.getHospSelect(value)
						this.formRef.reset(['hosp_id'])
					}
				}
			},
			{ label: '送检单位',		name: 'hosp_id', 			half:true,	type: 'select',	loading:true,	required:true,	nameStr: 'hosp_name' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
		this.getSelect()
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'lis-outsourcing-hosp/index')
	
	// 获取下拉数据
	getSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 外包公司
			$fn.setRow(formData, 'vendor_comp_id',  await f.getCache({ api:'lis-outsourcing-map/company' }) )
			//
			this.setState({ formData })
		})
	}
	// 获取送检单位
	getHospSelect = value => {
		$http.submit(null, 'lis-outsourcing-hosp/select', { param: {comp_id: value}, isPull:true }).then(data=>{
			const { formData } = this.state
			if(Array.isArray(data)){
				$cache.then( async f => {
					// 所属类型
					$fn.setRow(formData, 'hosp_id', data)
					//
					this.setState({ formData })
				})
			}
		})
	}
	// 表单
	cols = [
		{ title: '公司',		dataIndex: 'comp_name', 		width:50 },
		{ title: '分公司', 		dataIndex: 'vendor_comp_name', 	width:150 },
		{ title: '送检单位', 	dataIndex: 'hosp_name', 		width:150 },
		TableControls.controls.call(this, { width:100, align:'left'})
	]
	
	// 删除
	onDel = uuid => Delete.call(this, 'lis-outsourcing-hosp/del', { uuid, isKeys: !uuid, disabled: this.props.disabled })
	
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'lis-outsourcing-hosp/info', { uuid }, { load: ({vendor_comp_id}) => this.getHospSelect(vendor_comp_id) }),				// 编辑
		]
	}
	// 操作按钮
	controls = () => {
		const { selectKeys } = this.state
		return [
			Controls.add.call(this),			// 添加
			this.onDel(selectKeys)						// 删除
		]
	}
	
	render(){
		return (
			<>
				<Page title='集团内部外包设置' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'lis-outsourcing-hosp/add', editApi:'lis-outsourcing-hosp/edit', labelWidth:'s6' }) }
			</>
		)
	}
}

export default Index